<?php
namespace app\controllers\h5;

use app\controllers\h5\base\H5Controller;
use app\models\Brand as Model;
use app\models\product\Product;
use app\redis\Cache;

/**
 * Class BrandController
 * @package app\controllers\h5
 */
class BrandController extends H5Controller
{
    /**
     * brand list
     *
     * @return array|string[]|\yii\web\Response
     */
    public function actionList()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $key = Cache::$BRAND . $page;
        $cache = Cache::get($key);
        if (!$cache) {
            $query = Model::find()
                ->where(['status' => Model::$statusMapping['OK']]);

            $totalCount = $query->count();
            $totalPage = ceil($totalCount / $pageSize);

            $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
                ->offset(($page - 1) * $pageSize)
                ->limit($pageSize)
                ->with(['image'])
                ->asArray()
                ->all();

            Cache::set($key, json_encode([
                'list' => $list,
                'totalCount' => $totalCount,
                'totalPage' => $totalPage
            ]));
        } else {
            $data = json_decode($cache, true);
            $list = $data['list'];
            $totalCount = $data['totalCount'];
            $totalPage = $data['totalPage'];
        }

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    /**
     * brand view
     *
     * @return array|string[]|\yii\web\Response
     */
    public function actionView()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $brand_id = intval($this->post('brand_id'));

        $key = Cache::$BRAND_LIST . $brand_id . '-' . $page;
        $cache = Cache::get($key);

        if (!$cache) {
            // brand name
            $title = Model::find()
                ->where(['status' => Model::$statusMapping['OK']])
                ->andWhere(['id' => $brand_id])
                ->select(['name'])
                ->scalar();

            $query = Product::find()
                ->where(['status' => Product::$statusMapping['OK']])
                ->andWhere(['brand_id' => $brand_id]);

            $totalCount = $query->count();
            $totalPage = ceil($totalCount / $pageSize);

            $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
                ->offset(($page - 1) * $pageSize)
                ->limit($pageSize)
                ->with(['mainImage', 'scoreCost'])
                ->asArray()
                ->all();

            Cache::set($key, json_encode([
                'title' => $title,
                'totalCount' => $totalCount,
                'totalPage' => $totalPage,
                'list' => $list
            ]));
        } else {
            $data = json_decode($cache, true);
            $title = $data['title'];
            $totalCount = $data['totalCount'];
            $totalPage = $data['totalPage'];
            $list = $data['list'];
        }

        return $this->successEncode([
            'title' => $title,
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }
}